#include "AssetSelector.h"
#include "AssetPool.h"
#include "AssetModel.h"
#include "SelectorModel.h"
#include "..\core\src\AssetManager.h"

namespace Xps
{
	AssetSelector::AssetSelector(QWidget* parent, QString title, QString rsc, SelectorModel* model) : QDialog(parent)
	{
		setFixedSize(400,350);
		
		content = setupContent(model);
		QWidget* buttonpanel = setupButtonPanel();
		head = setupHeader(title, rsc);
		QVBoxLayout* layout = new QVBoxLayout();
		
		QDialog::setWindowIcon(QIcon(rsc));
		QDialog::setWindowTitle("Asset Pool");
		setLayout(layout);
		setModal(true);

		layout->addWidget(head);
		layout->addWidget(content);
		layout->addWidget(buttonpanel);
		layout->setMargin(0);
		layout->setSpacing(0);

			QPalette p(content->palette());
		p.setColor(QPalette::Background, QColor(255,255,255));
		content->setPalette(p);

		connect(okay, SIGNAL(clicked()), this, SLOT(accept()));
		connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
	}

	AssetSelector::~AssetSelector(void)
	{
	}

	QWidget* AssetSelector::setupButtonPanel()
	{

		QWidget* buttonpanel = new QWidget();
		okay = new QPushButton("Select");
		cancel = new QPushButton("Cancel");
		QHBoxLayout* buttonpanel_layout = new QHBoxLayout();
		buttonpanel->setLayout(buttonpanel_layout);
		buttonpanel_layout->insertStretch(0,100);
		buttonpanel_layout->addWidget(cancel);
		buttonpanel_layout->addWidget(okay);
		buttonpanel->setFixedHeight(40);

		setTabOrder(okay,cancel);
		okay->setDefault(true);
		okay->setEnabled(false);

		return buttonpanel;
	}

	QWidget* AssetSelector::setupContent(QAbstractItemModel* model)
	{

		QWidget* content = new QWidget();

		lv = new QTableView();
		


		content->setStyleSheet(".QWidget {background: white; color: black; }");
		lv->setStyleSheet("QTableView::item { padding: 5px; } QTableView { font: 11px; }");//:hover {     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,                                 stop: 0 #FAFBFE, stop: 1 #DCDEF1); }");
		//lv->setStyle(new QCleanlooksStyle());
		//lv->setStyleSheet("td { padding: 30px; }");
		//lv->setStyleSheet(" QTableView::item:hover {     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #FAFBFE, stop: 1 #DCDEF1); }; QTableView::item { padding: 50px; margin: 30px; };");
		QGridLayout* content_layout = new QGridLayout();
		//
		AssetModel* tm = ((Xps::AssetPool*) parent())->tm;
		lv->setModel(model);//tm->qm);
		lv->verticalHeader()->hide();
		lv->horizontalHeader()->hide();
		lv->setShowGrid(false);
		lv->horizontalHeader()->setStretchLastSection(true);
		lv->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
		lv->setSelectionBehavior(QAbstractItemView::SelectRows);
		lv->setSelectionMode(QAbstractItemView::SingleSelection);
		lv->setIconSize(QSize(20,20));
		lv->setAlternatingRowColors(true);
		lv->setWordWrap(true);
		content_layout->addWidget(lv, 1, 0);
		content_layout->addWidget(new QLabel("Choose an Asset to proceed."),0, 0);


		content->setLayout(content_layout);


		connect(lv->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(validateSelection()));
		return content;
	}

	void AssetSelector::validateSelection()
	{
		if (lv->selectionModel()->selectedIndexes().count() != 1)
		{
			okay->setEnabled(false);
			return;
		}

		okay->setEnabled(true);
	}

	QWidget* AssetSelector::setupHeader(QString head_text, QString head_rsc)
	{
		QFont headfont("Arial", 16, QFont::Bold);
		QLabel* head = new QLabel();
		QLabel* img = new QLabel(head);
		QLabel* text = new QLabel(head_text,head);
		
		text->setFont(headfont);
		text->setStyleSheet("background: none");

		img->setFixedWidth(80);
		img->setPixmap(QPixmap(head_rsc));
		img->setStyleSheet("background: none");
		
		head->setFixedHeight(80);
		head->setLayout(new QHBoxLayout());
		head->setBaseSize(500,80);
		head->layout()->addWidget(img);
		head->layout()->addWidget(text);
		head->setStyleSheet("background: url(:/rs/images/bg.png)");

		return head;
	}
	int AssetSelector::selectedAsset()
	{
		if (lv->selectionModel()->selectedIndexes().count() != 1)
			return -1;

		AssetModel* tm = ((Xps::AssetPool*) parent())->tm;
		return lv->model()->data(lv->selectionModel()->selectedIndexes().at(0), Qt::UserRole).toInt();
	}

	void AssetSelector::accept()
	{
		QDialog::accept();
	}
}